package com.Jian.reggie.service.impl;

import com.Jian.reggie.dto.DishDto;
import com.Jian.reggie.impl.DishFlavor;
import com.Jian.reggie.service.DishFlavorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.Jian.reggie.impl.Dish;
import com.Jian.reggie.service.DishService;
import com.Jian.reggie.mapper.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 12646
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2023-11-27 15:07:20
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService {
  @Autowired
  private DishFlavorService dishFlavor;
  
  // 新增菜品，同时保存对应口味数据
  @Override
  public void saveWithFlavor(DishDto dishDto) {
    // 保存菜品到Dish表
    this.save(dishDto);
    // 保存菜品口味
    dishDto.getFlavors().forEach(flavor -> {
      flavor.setDishId(dishDto.getId());
    });
    dishFlavor.saveBatch(dishDto.getFlavors());
  }
  
  @Override
  public void updateWithFlavor(DishDto dishDto) {
    // 更新菜品到Dish表
    this.updateById(dishDto);
    // 删除原来的菜品口味
    LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
    dishFlavor.remove(queryWrapper);
    // 保存菜品口味
    dishDto.getFlavors().forEach(flavor -> {
      flavor.setDishId(dishDto.getId());
    });
    dishFlavor.saveBatch(dishDto.getFlavors());
    
  }
}




